package cn.com.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;


public class Express100 {
	private static  String key="f012f1f8256929f7";
	public static String getExpressInfo(String com ,String nu){
		String ret = "";
		try
		{
			StringBuilder sb = new StringBuilder();
			sb.append("http://api.kuaidi100.com/api?id=");
			sb.append(key);
			sb.append("&com=").append(com);
			sb.append("&nu=").append(nu);
			sb.append("&show=0&muti=1&order=desc");
			URL url= new URL(sb.toString());
			URLConnection con=url.openConnection();
			 con.setAllowUserInteraction(false);
			   InputStream urlStream = url.openStream();
			   String type = con.guessContentTypeFromStream(urlStream);
			   String charSet=null;
			   if (type == null)
			    type = con.getContentType();

			   if (type == null || type.trim().length() == 0 || type.trim().indexOf("text/html") < 0)
			    return "";

			   if(type.indexOf("charset=") > 0)
			    charSet = type.substring(type.indexOf("charset=") + 8);

			   byte b[] = new byte[10000];
			   int numRead = urlStream.read(b);
			  String content = new String(b, 0, numRead);
			   while (numRead != -1) {
			    numRead = urlStream.read(b);
			    if (numRead != -1) {
			     //String newContent = new String(b, 0, numRead);
			     String newContent = new String(b, 0, numRead, charSet);
			     content += newContent;
			    }
			   }
			   ret = content;
			   urlStream.close();
		} catch (MalformedURLException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return ret;
	}
	
	
	
	public static String searchkuaiDiInfo(String com, String nu)
    {
        String content = "";
        try
        {
        	StringBuilder sb = new StringBuilder();
        	sb.append("http://www.kuaidi100.com/applyurl?key=").append(key);
        	sb.append("&com=").append(com);
        	sb.append("&nu=").append(nu);
            URL url = new URL(sb.toString());
            URLConnection con = url.openConnection();
            con.setAllowUserInteraction(false);
            InputStream urlStream = url.openStream();
            byte b[] = new byte[10000];
            int numRead = urlStream.read(b);
            content = new String(b, 0, numRead);
            while (numRead != -1)
            {
                numRead = urlStream.read(b);
                if (numRead != -1)
                {
                    // String newContent = new String(b, 0, numRead);
                    String newContent = new String(b, 0, numRead, "UTF-8");
                    content += newContent;
                }
            }
            urlStream.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
           // System.out.println("��ݲ�ѯ����");
        }
        return content;
    }
}
